//
//  LayerGroupOpacityViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/10.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "LayerGroupOpacityViewController.h"

@interface LayerGroupOpacityViewController ()

@property (nonatomic, strong) UIView *containerView;

@end

@implementation LayerGroupOpacityViewController

/**
 图层颜色的合成公式: 视图的颜色是从子视图到父视图(其实是cpu先计算每个图层的alpha，然后按照顶层视图到底层的顺序进行颜色的合成)
 R = S + D * ( 1 – Sa )
 
 *  当opaque属性被设为YES时，GPU就不会再利用图层颜色合成公式去合成真正的色值。即图层不会在计算该图层下方的颜色值了。这个可以提高性能。
 An opaque view is expected to fill its bounds with entirely opaque content—that is, the content should have an alpha value of 1.0. If the view is opaque and either does not fill its bounds or contains wholly or partially transparent content,the results are unpredictable. You should always set the value of this property to NO if the view is fully or partially transparent.
 大家切记！！！！
 
 四、shouldRasterize
 
 你可以设置CALayer的一个叫做shouldRasterize属性）来实现组透明的效果，如果它被设置为YES，在应用透明度之前，图层及其子图层都会被整合成一个整体的图片，这样就没有透明度混合的问题了(不需要每一帧重新合成了)，也就不会有因为子图层之间的混合和过度绘制的性能问题了。
 
 为了启用shouldRasterize属性，我们设置了图层的rasterizationScale属性。默认情况下，所有图层拉伸都是1.0， 所以如果你使用了shouldRasterize属性，你就要确保你设置了rasterizationScale属性去匹配屏幕，以防止出现Retina屏幕像素化的问题
 */

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.containerView = [[UIView alloc] initWithFrame:self.view.frame];
    self.containerView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    [self.view addSubview:_containerView];
    
    //create opaque button
    UIButton *button1 = [self customButton];
    button1.center = CGPointMake(100, 150);
    [self.containerView addSubview:button1];
    
    //create translucent button
    UIButton *button2 = [self customButton];
    button2.center = CGPointMake(280, 150);
    button2.alpha = 0.5;
    [self.containerView addSubview:button2];
    

    
    
    UIButton *button3 = [self customButton];
    button3.center = CGPointMake(280, 250);
    button3.alpha = 0.5;
    //enable rasterization for the translucent button
    button3.layer.shouldRasterize = YES;
    button3.layer.rasterizationScale = [UIScreen mainScreen].scale;
    [self.containerView addSubview:button3];
    
    
    // greenView
    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(100, 350, 160, 100)];
    greenView.backgroundColor = [UIColor greenColor];
    greenView.alpha = 0.8;
    [self.containerView addSubview:greenView];
    
    // redView
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(80, 420, 200, 100)];
    redView.backgroundColor = [UIColor redColor];
    redView.alpha = 0.8;
    [self.containerView addSubview:redView];
    
}

- (UIButton *)customButton
{
    //create button
    CGRect frame = CGRectMake(0, 0, 150, 50);
    UIButton *button = [[UIButton alloc] initWithFrame:frame];
    button.backgroundColor = [UIColor whiteColor];
    button.layer.cornerRadius = 10;
    
    //add label
    frame = CGRectMake(20, 10, 110, 30);
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.backgroundColor = [UIColor whiteColor];
    label.text = @"Hello World";
    label.textAlignment = NSTextAlignmentCenter;
    [button addSubview:label];
    return button;
}



@end
